<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
}
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'订单列表'),
); 
$return['status']=$this->order_status;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['paytype']['offpay']='线下收款';
$return['paytype']['offsao']='线下扫码';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['orderno'])){
		$params['orderno like']='%'.trim($_RQ['orderno']).'%';
		$return['params']['orderno']=$_RQ['orderno'];
	}
	if(!empty($_RQ['nickname'])){
		$data['uniacid']=$_SESSION['uniacid'];
		$data['nickname like']='%'.trim($_RQ['nickname']).'%';
		$headsid=pdo_getcolumn('core_members',$data,'id');
		$params['headsid']=intval($headsid);
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','cid','isverify','goodsid','groupid','teamid','orderno','groupnum','paytime','pay_type','status','price','dispatchid','addressid','addressid','express','sendtime','starttime','endtime','createtime');
	$list=pdo_getslice('jy_aligroup_groups_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$group=pdo_get('jy_aligroup_groups',array('id'=>$item['groupid'],'uniacid'=>$_SESSION['uniacid']));
			$team=pdo_get('jy_aligroup_groups_team',array('id'=>$item['teamid'],'uniacid'=>$_SESSION['uniacid']));
			$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid'],'uniacid'=>$_SESSION['uniacid']));
			$address=pdo_get('core_address',array('id'=>$item['addressid'],'uniacid'=>$_SESSION['uniacid']));
			$address['pcd']=json_decode($address['pcd'],true);
			$item['address']=$address['pcd']['province'].$address['pcd']['city'].$address['pcd']['district'].$address['address'];
			$corps=pdo_get('jy_aligroup_groups_corps',array('id'=>$item['cid'],'uniacid'=>$_SESSION['uniacid']));			
			$item['grouptitle']=$group['title'];
			$item['corpstitle']=$corps['title'];
			$item['goodstitle']=$goods['title'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'orderno','title'=>'订单编号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'grouptitle','title'=>'活动名称','type'=>'string'),
			//array('name'=>'corpstitle','title'=>'战队名','type'=>'string'),
			array('name'=>'goodstitle','title'=>'商品名称','type'=>'string'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'pay_type','title'=>'支付方式','type'=>'radio','options'=>'paytype'),
			//array('name'=>'coupon','title'=>'优惠券','type'=>'string'),
			array('name'=>'address','title'=>'收货地址','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
		),'action'=>array(
			array('action'=>'/order_express/','title'=>'查看物流','type'=>'modal','show'=>array('status'=>2,'expressid >'=>0)),
			array('op'=>'fa','title'=>'发货','type'=>'modal','show'=>array('status'=>2,'isverify'=>0)),
			array('op'=>'fa','title'=>'核销','type'=>'modal','show'=>array('status'=>2,'isverify'=>1)),
		)),
		'search'=>array(
			array('name'=>'orderno','title'=>'订单编号','type'=>'string','width'=>15),
			array('name'=>'nickname','title'=>'用户昵称','type'=>'string','width'=>15),
			array('name'=>'status','title'=>'订单状态','type'=>'select','width'=>15,'options'=>'selectstatus'),
		)
	);
	$return['selectstatus']=array(0=>array('id'=>'0','title'=>'待支付'),1=>array('id'=>'1','title'=>'待发货'),2=>array('id'=>'2','title'=>'待收货'),3=>array('id'=>'3','title'=>'已完成'),4=>array('id'=>'4','title'=>'已关闭'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='fa'){
	$install = check_account_permission('jy_peisong');
	if(!$install){
		exi('请联系管理员开通《配送员》插件！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['sendtype']==1){
			if($post['express_type']==0){
				if(!$post['express'] || !$post['expresssn']){
					exi('请填写快递信息','error');
				}
				$data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
				$this->model('order')->order_send($id,$data);
			}elseif($post['express_type']==1){//平台配送
				if(!$post['paisong_id'] && $post['need_setpeisong']){
					exi('请选择配送员！','error');
				}
				if($post['paisong_id']){
					$params['order_id']=$id;
					$params['peisong_id']=$post['paisong_id'];
					model('jy_peisong/order')->order_get($params);
				}else{
					$data['send_status']=12;
					cfc('jinyun_socket',$_SESSION['uniacid'])->send2room('刷新配送订单！','peisong_order');
					$this->model('order')->order_send($id,$data);
				}			
				exi('订单配送处理成功！','','//list/');
			}elseif($post['express_type']==2){//商家自己配送
				$this->model('order')->order_send($id);
				exi('订单配送处理成功！','','//list/');
			}elseif($post['express_type']==3){//达达配送
				$res=$this->model('order/dada_api')->dada_send($post);
				if($res['state']==1){
					$data=array('opensend_type'=>1);
					$this->model('order')->order_send($id,$data);
				}else{
					exi($res['msg'],'error');
				}
			}	
		}elseif($post['sendtype']==3){
			$this->model('order')->order_send_virtual($id,$post['content']);
		}elseif($post['sendtype']==2){
			$this->model('order')->order_verify($post['verifycode']);
		}		
		exi('已发货！','','/order/list/');
	}
	$id=intval($_RQ['id']);
	$item=$this->pdo_get('groups_order',array('id'=>$id));
	$address=pdo_get('core_address',array('id'=>$item['addressid'],'uniacid'=>$_SESSION['uniacid']));
	$address['pcd']=json_decode($address['pcd'],true);
	$address['addressinfo']=$address['pcd']['province'].' '.$address['pcd']['city'].' '.$address['pcd']['district'].' '.$address['pcd']['street'].' '.$address['address'];
	$item['address']=$address;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单发货',
	);
	$goods=pdo_get('jy_weishop_goods',array('id'=>$item['goodsid']));
	if($item['isverify']==0 && $goods['type']==0 && !$goods['isverify']){
		$item['sendtype']=1;
		$citypeisong=$this->plugin_setting('citypeisong');
		$item['citypeisong_isopen']=$citypeisong['isopen'];
		$item['citypeisong_express_type']=$citypeisong['express_type']?:0;
		if($citypeisong['isopen']==1 || $item['isdish']){
			$item['express_type']=$citypeisong['express_type']?:1;
			if($item['express_type']==1){
				$tip='商品订单已打包好,由平台配送员配送！';
			}elseif($item['express_type']==2){
				$tip='商品订单已打包好,商家自己配送,无需配送员！';
			}elseif($item['express_type']==3){
				$tip='商品订单已打包好,发送给达达配送！(<red>【慎重操作】：确定要呼叫《达达配送员》过来拿货吗?</red>)';
			}
		}else{
			$item['express_type']=0;
			$tip='商品订单已打包好,快递发货！';
		}
		$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'copy','title'=>'联系人','name'=>'address.name'),
			array('type'=>'copy','title'=>'联系电话','name'=>'address.mobile'),
			array('type'=>'copy','title'=>'收货地址','name'=>'address.addressinfo'),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('express_type'=>0)),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('express_type'=>0)),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定配送员？','show'=>array('express_type'=>1),'help'=>'不指定，则由配送员自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定配送员','name'=>'paisong_id','options'=>'peisongs','option_value'=>'mid','show'=>array('need_setpeisong'=>1),'help'=>'只能指定该门店下面的配送员，或本城市的平台配送员！'),
		);
		$return['express']=model('jy_weishop/model')->express_types();
	}else{
		$item=array_merge($goods,$item);
		$item['sendtype']=2;
		$item['content']='';
		$htmls['edit']=array(
			array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
			array('type'=>'image','title'=>'商品图片','name'=>'image','read'=>true),
			array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
			array('type'=>'text','title'=>'发货内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品发货，填写发货内容！'),
			
		);
	}
	$peisongs=array();
	//多商户用户，选择平台区域配送员
	if($item['merchid']){
		$areas=pdo_getall('jy_peisong_area',array('store_id'=>0));
	}else{
		$areas=pdo_getall('jy_peisong_area',array('store_id'=>array($item['storeid'],0)));
	}
	foreach($areas as $v){
			$users=pdo_getall('jy_peisong_member',array('group_id'=>$v['id'],'status'=>1,'online >'=>0),array('mid','title'));
			foreach($users as $uv){
					$peisongs[]=$uv;
			}
	}
	$return['peisongs']=$peisongs;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}